package com.shqcjd.yunweiyanoperator.ui.activity.guest;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.pgyersdk.crash.PgyCrashManager;
import com.pgyersdk.update.PgyUpdateManager;
import com.shqcjd.yunweiyanoperator.R;
import com.shqcjd.yunweiyanoperator.app.AppConstant;
import com.shqcjd.yunweiyanoperator.jpush.TagAliasUtils;
import com.shqcjd.yunweiyanoperator.model.eventbus.TokenOvertimeEvent;
import com.shqcjd.yunweiyanoperator.model.pojo.GoRegister;
import com.shqcjd.yunweiyanoperator.ui.activity.LoginActivity;
import com.shqcjd.yunweiyanoperator.ui.activity.staff.MainActivity;
import com.shqcjd.yunweiyanoperator.ui.adapter.FragPagerAdapter;
import com.shqcjd.yunweiyanoperator.ui.base.BaseActivity;
import com.shqcjd.yunweiyanoperator.ui.base.BasePresenter;
import com.shqcjd.yunweiyanoperator.ui.fragment.MineFragment;
import com.shqcjd.yunweiyanoperator.ui.fragment.RemoteGuideFragment;
import com.shqcjd.yunweiyanoperator.ui.fragment.SquareFragment;
import com.shqcjd.yunweiyanoperator.util.AppManager;
import com.shqcjd.yunweiyanoperator.util.PrefUtils;
import com.shqcjd.yunweiyanoperator.util.ToastUtils;
import com.shqcjd.yunweiyanoperator.util.UIUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 游客主activity
 */
public class GuestMainActivity extends BaseActivity {
    private List<Fragment> mFragments = new ArrayList<>();

    @BindView(R.id.viewpager)
    ViewPager viewpager;
    @BindView(R.id.iv_home)
    ImageView ivHome;
    @BindView(R.id.tv_home)
    TextView tvHome;
    @BindView(R.id.ll_home)
    LinearLayout llHome;
    @BindView(R.id.iv_square)
    ImageView ivSquare;
    @BindView(R.id.tv_square)
    TextView tvSquare;
    @BindView(R.id.ll_square)
    LinearLayout llSquare;
    @BindView(R.id.iv_mine)
    ImageView ivMine;
    @BindView(R.id.tv_mine)
    TextView tvMine;
    @BindView(R.id.ll_mine)
    LinearLayout llMine;
    private Context mContext;
    private boolean isFirst = true;

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_main;
    }

    @Override
    public void initView() {
        EventBus.getDefault().register(this);//订阅
        mContext = this;
        setTabColor(ivHome,tvHome);
        initFragment();

        initPgy();
    }

    private void initPgy() {
        //蒲公英
        PgyCrashManager.register(); //推荐使用新版
        /** 蒲公英可选配置集成方式 **/
        new PgyUpdateManager.Builder()
                .setForced(false)                //设置是否强制提示更新
                // v3.0.4+ 以上同时可以在官网设置强制更新最高低版本；网站设置和代码设置一种情况成立则提示强制更新
                .setUserCanRetry(false)         //失败后是否提示重新下载
                .setDeleteHistroyApk(true)     // 检查更新前是否删除本地历史 Apk， 默认为true
                .register();
    }
    private void setTabColor(ImageView icon, TextView textView) {
        cleanSelect();
        switch (icon.getId()) {
            case R.id.iv_home:
                icon.setBackgroundResource(R.mipmap.ic_home_checked);
                break;
            case R.id.iv_square:
                icon.setBackgroundResource(R.mipmap.ic_square_checked);
                break;
            case R.id.iv_mine:
                icon.setBackgroundResource(R.mipmap.ic_mine_checke);
                break;
        }
        textView.setTextColor(UIUtils.getColor(R.color.main_tab_checked));
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if(isFirst){
            isFirst = false;
            viewpager.setCurrentItem(1);
            setTabColor(ivSquare,tvSquare);
        }
    }

    private void cleanSelect(){
        ivHome.setBackgroundResource(R.mipmap.ic_home_checked_no);
        tvHome.setTextColor(UIUtils.getColor(R.color.main_tab_checked_no));
        ivSquare.setBackgroundResource(R.mipmap.ic_square_checked_no);
        tvSquare.setTextColor(UIUtils.getColor(R.color.main_tab_checked_no));
        ivMine.setBackgroundResource(R.mipmap.ic_mine_checke_no);
        tvMine.setTextColor(UIUtils.getColor(R.color.main_tab_checked_no));
    }

    private void initFragment() {
        mFragments.add(RemoteGuideFragment.newInstance());
        mFragments.add(SquareFragment.newInstance());
        mFragments.add(MineFragment.newInstance());

        viewpager.setAdapter(new FragPagerAdapter(getSupportFragmentManager(), mFragments));
        viewpager.setCurrentItem(0, false);
        viewpager.setOffscreenPageLimit(2);
        viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        setTabColor(ivHome, tvHome);
                        break;
                    case 1:
                        setTabColor(ivSquare,tvSquare);
                        break;
                    case 2:
                        setTabColor(ivMine,tvMine);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @OnClick({R.id.ll_home, R.id.ll_square, R.id.ll_mine})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_home://远程指导
                viewpager.setCurrentItem(0);
                setTabColor(ivHome,tvHome);
                break;
            case R.id.ll_square://广场
                viewpager.setCurrentItem(1);
                setTabColor(ivSquare,tvSquare);
                break;
            case R.id.ll_mine://个人中心
                viewpager.setCurrentItem(2);
                setTabColor(ivMine,tvMine);
                break;
        }
    }
    public long preTime;
    public final long TWO_SECOND = 2 * 1000;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            long currentTime = System.currentTimeMillis();
            if (currentTime - preTime > TWO_SECOND) {
                ToastUtils.show(GuestMainActivity.this, "再按一次退出程序", Toast.LENGTH_SHORT);
                // 更新时间
                preTime = System.currentTimeMillis();

            } else {
                GuestMainActivity.this.finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onTokenOvertimeEvent(TokenOvertimeEvent event){//token超时，重新登录
        ToastUtils.showLong(mContext,"登录信息过期,请重新登录");
        goRegister();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onGoRegister(GoRegister goRegister){//游客去注册
        goRegister();
    }


    /**
     * 游客去注册
     */
    private void goRegister() {
        PrefUtils.setString(mContext, AppConstant.TOKEN_KEY, "");
        PrefUtils.setString(mContext, PrefUtils.USER_NAME, "");
        PrefUtils.setInt(mContext, PrefUtils.USER_TYPE, -1);
        AppManager.getAppManager().finishAllActivity();
        Intent it = new Intent(this, LoginActivity.class);
        startActivity(it);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);//解除订阅
    }
}
